$(function () {
    $.ajax({
        url: Feng.ctxPath + "/consultant/getDoctor",
        type: "get",
        success: function (data) {
            var txt = '<option value="">--请选择医生--</option>';
            for (var i = 0; i < data.length; i++) {txt += '<option value="' + data[i].doctor + '">' + data[i].doctor + '</option>';}
            document.getElementById('doctor').innerHTML = txt;
            var seleteDoctorValue = $('#userName').val();
            if (seleteDoctorValue != "") {$("#doctor option[value=" + seleteDoctorValue + "]").attr("selected", "selected");}
        },error: function (data) {alert('没有权限，请联系管理员!');}
    });
    $.ajax({
        url: Feng.ctxPath + "/outpatientManagement/getList",
        type: "post",
        success: function (data) {
            var txt = '<option value="">--请选择门店--</option>';
            for (var i = 0; i < data.length; i++) {txt += '<option value="' + data[i].simplename + '">' + data[i].simplename + '</option>';}
            document.getElementById('arrivalStores').innerHTML = txt;
            var seleteStoreValue = $('#storeValue').val();
            if (seleteStoreValue != "") {$("#arrivalStores option[value=" + seleteStoreValue + "]").attr("selected", "selected");}
        },error: function (data) {alert('没有权限，请联系管理员!');}
    });
});

cancel.onclick = function() {parent.layer.close(parent.layer.getFrameIndex(window.name));};

machiningNumbers = function(obj){
    $(obj).val($(obj).val().replace(/[^\d]/g, ''));
    var price = $(obj).parent().siblings().children('.price').val();
    var amount1 = parseInt(price) * parseInt($(obj).val());
    if(isNaN(amount1) !=true){
        $(obj).parent().siblings().children('.amount').val(amount1);
        $(obj).parent().siblings().children('.amountActually').val(amount1);
    }
};
maniningPrices = function(obj){
    $(obj).val($(obj).val().replace(/[^\d]/g, ''));
    var num = $(obj).parent().siblings().children('.number').val();
    var amount1 = parseInt(num) * parseInt($(obj).val());
    if(isNaN(amount1) !=true){
        $(obj).parent().siblings().children('.amount').val(amount1);
        $(obj).parent().siblings().children('.amountActually').val(amount1)
    }
};

ensure.onclick = function () {
    var leftUp = $(".leftUp").val();
    var rightUp = $(".rightUp").val();
    var rightDown = $(".rightDown").val();
    var leftDown = $(".leftDown").val();
    var consumableName = $(".consumableName").val();
    var arrivalStore = $(".arrivalStore").val();
    var number = $(".number").val();
    var price = $(".price").val();
    var amount = $(".amount").val();
    var amountActually = $(".amountActually").val();
    var type = $(".type").val();
    var doctor = $("#doctor").val();
    var remarks = $("#remarks").val();
    var id = $("#id").val();
    if($(".consumableName").val()==""||$(".consumableName").val()==null){alert("耗材不能为空");return;}
    if($(".number").val()==""||$(".number").val()==null){alert("数量类别不能为空");return;}
    if($(".amount").val()==""||$(".amount").val()==null){alert("金额不能为空");return;}
    if($(".amountActually").val()==""||$(".amountActually").val()==null){alert("实付金额不能为空");return;}
    $.ajax({
        url: Feng.ctxPath + "/doctorLooked/update_consumable",
        type: "post",
        data: {
            doctor: doctor,
            leftUp: leftUp,
            rightUp:rightUp,
            remarks: remarks,
            rightDown:rightDown,
            arrivalStore:arrivalStore,
            leftDown:leftDown,
            consumableName: consumableName,
            number: number,
            price:price,
            type: type,
            id: id,
            amount:amount,
            amountActually:amountActually
        },
        success: function (data) {
            Feng.success('操作成功!');
            window.parent.TechnologyTableList.table.refresh();
            $("#cancel").click();
        },error: function (data) {alert('没有权限，请联系管理员!');}
    });
};
var index;
$(".machiningBox").on('click', '.toothBox', function (e) {
    e.stopPropagation();
    $(".toothBox").removeClass('tips');
    $(this).addClass('tips');
    var leftUpNum = $(this).find('.leftUp').val();
    var rightUpNum = $(this).find('.rightUp').val();
    var leftDownNum = $(this).find('.leftDown').val();
    var rightDownNum = $(this).find('.rightDown').val();
    index = layer.open({
        type: 4,
        tips: [1, '#ffffff'],
        closeBtn: 0,
        area: ['auto', 'auto'],
        content: ['<div>' + $("#noneBox").html() + '</div>', '.tips'],
        success : function(layero, index){
            if(leftUpNum.length > 0){
                for(var w=0 ; w<$(layero).find('.toothTopLeft>span').length ; w++){
                    for(var i=0 ; i<leftUpNum.length ; i++){
                        if($(layero).find('.toothTopLeft>span').eq(w).find('span').text() == leftUpNum.substring(i,i+1)){
                            $(layero).find('.toothTopLeft>span').eq(w).find('span').addClass('spanStyle');
                        }
                    }
                }
            }
            if(rightUpNum.length > 0){
                for(var w=0 ; w<$(layero).find('.toothTopRight>span').length ; w++){
                    for(var i=0 ; i<rightUpNum.length ; i++){
                        if($(layero).find('.toothTopRight>span').eq(w).find('span').text() == rightUpNum.substring(i,i+1)){
                            $(layero).find('.toothTopRight>span').eq(w).find('span').addClass('spanStyle');
                        }
                    }
                }
            }
            if(leftDownNum.length > 0){
                for(var w=0 ; w<$(layero).find('.toothBottomLeft>span').length ; w++){
                    for(var i=0 ; i<leftDownNum.length ; i++){
                        if($(layero).find('.toothBottomLeft>span').eq(w).find('span').text() == leftDownNum.substring(i,i+1)){
                            $(layero).find('.toothBottomLeft>span').eq(w).find('span').addClass('spanStyle');
                        }
                    }
                }
            }
            if(rightDownNum.length > 0){
                for(var w=0 ; w<$(layero).find('.toothBottomRight>span').length ; w++){
                    for(var i=0 ; i<rightDownNum.length ; i++){
                        if($(layero).find('.toothBottomRight>span').eq(w).find('span').text() == rightDownNum.substring(i,i+1)){
                            $(layero).find('.toothBottomRight>span').eq(w).find('span').addClass('spanStyle');
                        }
                    }
                }
            }
        }
    });
});

$(document).on('mouseleave','#tipsBox',function () {layer.close(index)});

$(document).on('click', '.tipsUl>li', function (e) {
    e.stopPropagation();$(this).addClass('tipsLi').siblings().removeClass('tipsLi');
    if ($(this).index() == 0) {$(this).parent().siblings().children('#permanent').show().siblings().hide();}
    else if ($(this).index() == 1) {$(this).parent().siblings().children('#deciduous').show().siblings().hide();}
});

$(document).on('click', '.toothTopLeft>span', function (e) {
    e.stopPropagation();
    var arr = [];
    var text = $(this).text();
    if ($(this).children().attr('class') == 'spanStyle') {
        var inputValue = $(".tips .leftUp").val().replace(text, "");
        for(var i=0 ; i<inputValue.length ; i++){arr.push(inputValue.substring(i,i+1));}
        for(var i=arr.length-1;i>=0;i--){for(var j=0;j<i;j++){if(arr[j+1]>arr[j]){var change=arr[j+1];arr[j+1]=arr[j];arr[j]=change;}}};
        var str = "";
        for(var i=0 ; i<arr.length ; i++){str += arr[i];}
        $(".tips .leftUp").val(str);
        $(this).children().removeClass('spanStyle');
    } else {
        var add = $(".tips .leftUp").val() + text;
        for(var d=0 ; d<add.length ; d++){arr.push(add.substring(d,d+1));}
        for(var i=arr.length-1;i>=0;i--){for(var j=0;j<i;j++){if(arr[j+1]>arr[j]){var change=arr[j+1];arr[j+1]=arr[j];arr[j]=change;}}};
        var str = "";
        for(var i=0 ; i<arr.length ; i++){str += arr[i];}
        $(this).children().addClass('spanStyle');
        $(".tips .leftUp").val(str)
    }
});

$(document).on('click', '.toothTopRight>span', function (e) {
    e.stopPropagation();
    var arr = [];
    var text = $(this).text();
    if ($(this).children().attr('class') == 'spanStyle') {
        var inputValue = $(".tips .rightUp").val().replace(text, "");
        for(var i=0 ; i<inputValue.length ; i++){arr.push(inputValue.substring(i,i+1));}
        for(var i=arr.length-1;i>=0;i--){for(var j=0;j<i;j++){if(arr[j+1]<arr[j]){var change=arr[j+1];arr[j+1]=arr[j];arr[j]=change;}}};
        var str = "";
        for(var i=0 ; i<arr.length ; i++){str += arr[i];}
        $(".tips .rightUp").val(str);
        $(this).children().removeClass('spanStyle');
    } else {
        $(this).children().addClass('spanStyle');
        var add = $(".tips .rightUp").val() + text;
        for(var d=0 ; d<add.length ; d++){arr.push(add.substring(d,d+1));}
        for(var i=arr.length-1;i>=0;i--){for(var j=0;j<i;j++){if(arr[j+1]<arr[j]){var change=arr[j+1];arr[j+1]=arr[j];arr[j]=change;}}};
        var str = "";
        for(var i=0 ; i<arr.length ; i++){str += arr[i];}
        $(".tips .rightUp").val(str);
    }
});

$(document).on('click', '.toothBottomLeft>span', function (e) {
    e.stopPropagation();
    var arr = [];
    var text = $(this).text();
    if ($(this).children().attr('class') == 'spanStyle') {
        var inputValue = $(".tips .leftDown").val().replace(text, "");
        for(var i=0 ; i<inputValue.length ; i++){arr.push(inputValue.substring(i,i+1));}
        for(var i=arr.length-1;i>=0;i--){for(var j=0;j<i;j++){if(arr[j+1]>arr[j]){var change=arr[j+1];arr[j+1]=arr[j];arr[j]=change;}}};
        var str = "";
        for(var i=0 ; i<arr.length ; i++){str += arr[i];}
        $(".tips .leftDown").val(str);
        $(this).children().removeClass('spanStyle');
    } else {
        $(this).children().addClass('spanStyle');
        var add = $(".tips .leftDown").val() + text;
        for(var d=0 ; d<add.length ; d++){arr.push(add.substring(d,d+1));}
        for(var i=arr.length-1;i>=0;i--){for(var j=0;j<i;j++){if(arr[j+1]>arr[j]){var change=arr[j+1];arr[j+1]=arr[j];arr[j]=change;}}};
        var str = "";
        for(var i=0 ; i<arr.length ; i++){str += arr[i];}
        $(".tips .leftDown").val(str);
    }
});

$(document).on('click', '.toothBottomRight>span', function (e) {
    e.stopPropagation();
    var arr = [];
    var text = $(this).text();
    if ($(this).children().attr('class') == 'spanStyle') {
        var inputValue = $(".tips .rightDown").val().replace(text, "");
        for(var i=0 ; i<inputValue.length ; i++){arr.push(inputValue.substring(i,i+1));}
        for(var i=arr.length-1;i>=0;i--){for(var j=0;j<i;j++){if(arr[j+1]<arr[j]){var change=arr[j+1];arr[j+1]=arr[j];arr[j]=change;}}};
        var str = "";
        for(var i=0 ; i<arr.length ; i++){str += arr[i];}
        $(".tips .rightDown").val(str);
        $(this).children().removeClass('spanStyle');
    } else {
        $(this).children().addClass('spanStyle');
        var add = $(".tips .rightDown").val() + text;
        for(var d=0 ; d<add.length ; d++){arr.push(add.substring(d,d+1));}
        for(var i=arr.length-1;i>=0;i--){for(var j=0;j<i;j++){if(arr[j+1]<arr[j]){var change=arr[j+1];arr[j+1]=arr[j];arr[j]=change;}}};
        var str = "";
        for(var i=0 ; i<arr.length ; i++){str += arr[i];};
        $(".tips .rightDown").val(str);
    }
});